#!/bin/bash
#
# This script sets up a Kokoro MacOS worker for running Protobuf tests

set -eux

export HOMEBREW_PREFIX=$(brew --prefix)

##
# Select Xcode version
export DEVELOPER_DIR=/Applications/Xcode_13.3.1.app/Contents/Developer
sudo xcode-select -s "${DEVELOPER_DIR}"

##
# Use Python 2 by default (for googletest)
pyenv global 2.7.18

##
# Install Tox

if [[ "${KOKORO_INSTALL_TOX:-}" == "yes" ]] ; then
  sudo python3 -m pip install --upgrade pip tox
fi

##
# Setup RVM
if [[ "${KOKORO_INSTALL_RVM:-}" == "yes" ]] ; then
    git config --global --add safe.directory $HOMEBREW_PREFIX/Library/Taps/homebrew/homebrew-cask
    git config --global --add safe.directory $HOMEBREW_PREFIX/Library/Taps/homebrew/homebrew-core
    git config --global --add safe.directory $HOMEBREW_PREFIX/Library/Taps/homebrew/homebrew-services
    sudo chown -R $(whoami) $HOME/.rvm/
fi

# "Install" valgrind if it doesn't exist
##
if [ ! -x "$(command -v valgrind)" ]; then
    echo "#! /bin/bash" > valgrind
    chmod ug+x valgrind
    sudo mv valgrind /usr/local/bin/valgrind
fi
